package com.example.blog_system.config;

import com.example.blog_system.common.AjaxResult;
import com.example.blog_system.common.EmailUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description: 统一的异常处理
 */

@ControllerAdvice
@ResponseBody
@Slf4j
public class ExceptionHandle {
    // 捕获所有异常
    @ExceptionHandler(Exception.class)
    public AjaxResult Exception(Exception e) {
        log.error("出现异常:{}",e);
        e.printStackTrace();
        EmailUtils.sendMyselfEmail("出现异常: "+ Arrays.toString(e.getStackTrace()));
        return AjaxResult.fail(-1, "异常信息: " + e.getMessage());
    }
}
